package com.mico.example.springboot.jpa.multi.service;


import com.mico.example.springboot.jpa.multi.domain.primary.User;
import com.mico.example.springboot.jpa.multi.domain.secondary.City;
import com.mico.example.springboot.jpa.multi.repository.primary.UserRepository;
import com.mico.example.springboot.jpa.multi.repository.secondary.CityRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * Created by micocube
 * ProjectName: springboot-example
 * PackageName: com.mico.example.springboot.jpa.service.impl
 * User: micocube
 * Email: ldscube@gmail.com
 * CreateTime: 2019/2/25下午1:52
 * ModifyTime: 2019/2/25下午1:52
 * Version: 0.1
 * Description:
 **/
@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl implements UserService{
    @Autowired
    UserRepository userRepository;
    @Autowired
    CityRepository cityRepository;
    @Override
    public Page<User> findByUsername(String name, Pageable pageable){
        return userRepository.findByUsername(name,pageable);
    }

    @Override
    public User findByUsername(String name) {
        User repository = userRepository.findByUsername(name);
        City city = cityRepository.findByCity("浙江");
        repository.setCity(city);
        return repository;
    }

    @Override
    public Page<User> findAll(Pageable pageable){
        return userRepository.findAll(pageable);
    }

    @Override
    public void test() {
        User user = new User("multiTest","123456");
        user.setEnabled(true);
        userRepository.save(user);

        City city = new City("aaa","bbb");
        cityRepository.save(city);

    }


    @Override
    public void testTransaction() {
        User user = new User("multiTest","123456");
        user.setEnabled(true);
        userRepository.save(user);

        City city = new City("aaa","bbb");
        cityRepository.save(city);

        System.out.println(1/0);

    }

}
